#!/bin/bash
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash goInstall.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1
[[ "$1" = "$2" ]] && echo "构建路径与安装路径不能相同" && exit 1
buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}

echo 'go语言安装'
wget -t 10 -c -P $buildpath https://golang.google.cn/dl/go1.18.2.linux-arm64.tar.gz
mkdir -p $installpath/go-1.18.2
tar -xzf $buildpath/go1.18.2.linux-arm64.tar.gz -C $installpath/go-1.18.2
mkdir -p $installpath/go-1.18.2/gopath
cd $installpath/go-1.18.2/gopath
mkdir src pkg bin
echo '写入go_modulefiles'
cat>"$installpath/go-1.18.2/go_modulefiles"<<EOF
#%Module1.0
conflict go
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set go \$pwd
setenv go \$go
prepend-path GOPATH \$go/gopath
prepend-path PATH \$go/go/bin
EOF



